﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class PhanQuyen : System.Web.UI.Page
{
    clsXuLy ex = new clsXuLy();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dlNhomND.DataSource = ex.GetData_Text("select * from vnttblnhomnguoidung order by tennhom");
            dlNhomND.DataBind();
            dlNhomND.Items.Insert(0, new ListItem("--Chọn nhóm người dùng--", ""));
            dlNhomND.SelectedIndex = 0;

            gvChucNang.DataSource = ex.GetData_Text("select IDs, TenChucNang,Tag from vnttblchucnang where tenchucnang is not null and Tag <> '#' order by chucnangcha,vitri");
            gvChucNang.DataBind();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            SqlParameter[] p;
            p = new SqlParameter[1];
            int IDNhomNguoiDung = int.Parse(dlNhomND.SelectedValue);
            p[0] = new SqlParameter("@NhomND", IDNhomNguoiDung);
            p[0].SqlDbType = SqlDbType.Int;
            ex.ExecuteNonQuerry("sp_vnttblnguoidungchucnang_Del", p);

            for (int i = 0; i < gvChucNang.Rows.Count; i++)
            {
                CheckBox ckSuDung = (CheckBox)gvChucNang.Rows[i].Cells[2].FindControl("ckSuDung");
                CheckBox ckThem = (CheckBox)gvChucNang.Rows[i].Cells[3].FindControl("ckThem");
                CheckBox ckSua = (CheckBox)gvChucNang.Rows[i].Cells[4].FindControl("ckSua");
                CheckBox ckXoa = (CheckBox)gvChucNang.Rows[i].Cells[5].FindControl("ckXoa");
                CheckBox ckPheduyet = (CheckBox)gvChucNang.Rows[i].Cells[6].FindControl("ckPheDuyet");
                CheckBox ckPrint = (CheckBox)gvChucNang.Rows[i].Cells[7].FindControl("ckIn");

                Label lbTag = (Label)gvChucNang.Rows[i].Cells[8].FindControl("lbTag");
                
                InsertPhanQuyen(IDNhomNguoiDung, lbTag.Text, ckThem.Checked ? 1 : 0, ckSua.Checked ? 1 : 0, ckXoa.Checked ? 1 : 0, ckSuDung.Checked ? 1 : 0, ckPheduyet.Checked ? 1 : 0, ckPrint.Checked ? 1 : 0);

            }
            gstGetMess("Lưu dữ liệu thành công !", "");
        }
        catch (Exception ax)
        {
            gstGetMess("Lỗi khi cập nhật dữ liệu: " + ax.Message, "");
        }
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    void InsertPhanQuyen(int NhomNguoiDung, string Tag, int them, int sua, int xoa, int hienthi,int pheduyet, int print)
    {
        SqlParameter[] p = new SqlParameter[8];
        p[0] = new SqlParameter("@NhomND", NhomNguoiDung);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@Tag", Tag);
        p[1].SqlDbType = SqlDbType.NVarChar;
        p[2] = new SqlParameter("@SuDung", hienthi);
        p[2].SqlDbType = SqlDbType.Int;
        p[3] = new SqlParameter("@Them", them);
        p[3].SqlDbType = SqlDbType.Int;
        p[4] = new SqlParameter("@Sua", sua);
        p[4].SqlDbType = SqlDbType.Int;
        p[5] = new SqlParameter("@Xoa", xoa);
        p[5].SqlDbType = SqlDbType.Int;
        p[6] = new SqlParameter("@PheDuyet", pheduyet);
        p[6].SqlDbType = SqlDbType.Int;
        p[7] = new SqlParameter("@Print", print);
        p[7].SqlDbType = SqlDbType.Int;
        ex.ExecuteNonQuerry("sp_vnttblnguoidungchucnang_Add", p);

    }

    protected void ckCheckAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < gvChucNang.Rows.Count; i++)
        {
            CheckBox ck1 = (CheckBox)gvChucNang.Rows[i].Cells[2].FindControl("ckSuDung");
            CheckBox ck2 = (CheckBox)gvChucNang.Rows[i].Cells[3].FindControl("ckThem");
            CheckBox ck3 = (CheckBox)gvChucNang.Rows[i].Cells[4].FindControl("ckSua");
            CheckBox ck4 = (CheckBox)gvChucNang.Rows[i].Cells[5].FindControl("ckXoa");
            CheckBox ck5 = (CheckBox)gvChucNang.Rows[i].Cells[5].FindControl("ckPheDuyet");
            CheckBox ck6 = (CheckBox)gvChucNang.Rows[i].Cells[5].FindControl("ckIn");
            if (ckCheckAll.Checked)
            {
                ck1.Checked = true; ck2.Checked = true; ck3.Checked = true; ck4.Checked = true; ck5.Checked = true; ck6.Checked = true;
            }
            else
            {
                ck1.Checked = false; ck2.Checked = false; ck3.Checked = false; ck4.Checked = false; ck5.Checked = false; ck6.Checked = false;
            }
        }
    }
    protected void dlNhomND_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlNhomND.SelectedIndex > 0)
        {
            try
            {
                SqlParameter[] p;
                p = new SqlParameter[1];
                int IDNguoiDung = int.Parse(dlNhomND.SelectedValue);
                p[0] = new SqlParameter("@NhomND", IDNguoiDung);
                p[0].SqlDbType = SqlDbType.Int;



                DataTable dt = ex.GetData("sp_vnttblnguoidungchucnang_ListByNhomND", p);


                if (dt.Rows.Count > 0)
                {
                    gvChucNang.DataSource = dt;
                    gvChucNang.DataBind();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Label lbTag = (Label)gvChucNang.Rows[i].Cells[8].FindControl("lbTag");
                        CheckBox ckSuDung = (CheckBox)gvChucNang.Rows[i].Cells[2].FindControl("ckSuDung");
                        CheckBox ckThem = (CheckBox)gvChucNang.Rows[i].Cells[3].FindControl("ckThem");
                        CheckBox ckSua = (CheckBox)gvChucNang.Rows[i].Cells[4].FindControl("ckSua");
                        CheckBox ckXoa = (CheckBox)gvChucNang.Rows[i].Cells[5].FindControl("ckXoa");

                        CheckBox ckPheduyet = (CheckBox)gvChucNang.Rows[i].Cells[6].FindControl("ckPheDuyet");
                        CheckBox ckPrint = (CheckBox)gvChucNang.Rows[i].Cells[7].FindControl("ckIn");

                        if (dt.Rows[i]["Tag"].ToString().Equals(lbTag.Text))
                        {
                            ckSuDung.Checked = dt.Rows[i]["SuDung"].ToString().Equals("1") ? true : false;
                            ckThem.Checked = dt.Rows[i]["Them"].ToString().Equals("1") ? true : false;
                            ckSua.Checked = dt.Rows[i]["Sua"].ToString().Equals("1") ? true : false;
                            ckXoa.Checked = dt.Rows[i]["Xoa"].ToString().Equals("1") ? true : false;
                            ckPheduyet.Checked = dt.Rows[i]["PheDuyet"].ToString().Equals("1") ? true : false;
                            ckPrint.Checked = dt.Rows[i]["Print"].ToString().Equals("1") ? true : false;
                        }
                    }
                }
                else
                {
                    gvChucNang.DataSource = ex.GetData_Text("select IDs, TenChucNang,Tag from vnttblchucnang where tenchucnang is not null and Tag <> '#' order by chucnangcha,vitri");
                    gvChucNang.DataBind();
                }
                ckCheckAll.Checked = false;
            }
            catch (Exception ax)
            { ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now + "\tPhân quyền\t" + ax.Message); }

        }
        else
        {
            gvChucNang.DataSource = ex.GetData_Text("select IDs, TenChucNang,Tag from vnttblchucnang where tenchucnang is not null and Tag <> '#' order by chucnangcha,vitri");
            gvChucNang.DataBind();
        }
    }
}